package com.gdpu.liou.symphonyserver.service.impl;

import com.gdpu.liou.symphonyserver.common.utils.FileUpload;

import com.gdpu.liou.symphonyserver.mapper.RankMapper;
import com.gdpu.liou.symphonyserver.mapper.SongListMapper;
import com.gdpu.liou.symphonyserver.entity.SongList;
import com.gdpu.liou.symphonyserver.service.SongListService;
import com.gdpu.liou.symphonyserver.common.utils.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class SongListServiceImpl implements SongListService {
    @Autowired
    FileUpload fileUpload;
    private final SongListMapper songListMapper;
    private final RankMapper rankMapper;

    public SongListServiceImpl(SongListMapper songListMapper, RankMapper rankMapper) {
        this.songListMapper = songListMapper;
        this.rankMapper = rankMapper;
    }

    @Override
    public SongList songListById(Long id) {
        SongList songList = songListMapper.songListById(id);
        Double i = rankMapper.selectAvgScoreOfPlaylist(id);
        long score = 0;
        if (i != null) {
            score = Math.round(i);
        }
        songList.setScore(score);
        return songList;
    }

    @Override
    public PagingUtil pagingQuerySongList(int currentPage, String title) {
        int totalCount = songListMapper.selectTotalSongList(title);
        int pageSize = 6;
        int pageCount = 0;
        pageCount = (totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1);
        // 数据起始位置
        int start = (currentPage - 1) * pageSize;
        List<SongList> data = songListMapper.selectPagingSongList(start, pageSize, title);
        PagingUtil pagingUtil = new PagingUtil(pageSize, totalCount, pageCount, currentPage, data);
        return pagingUtil;
    }

    @Override
    public List<SongList> selectAllSongList() {
        return songListMapper.selectAllSongList();
    }

    @Override
    public List<SongList> selectGoodSongList(){
        return songListMapper.selectGoodSongList();
    }

    @Override
    public List<SongList> songListOfTitle(String title) {
        return songListMapper.songListOfTitle(title);
    }

    @Override
    public List<SongList> likeTitle(String title) {
        return songListMapper.likeTitle(title);
    }

    @Override
    public List<SongList> likeStyle(String style) {
        return songListMapper.likeStyle(style);
    }

    @Override
    public List<SongList> songListByKeyword(String keyword, Integer limit){
        return  songListMapper.songListByKeyword(keyword, limit);
    }

    @Override
    public int addSongListFavourite(String id) {
        return songListMapper.incrementFavourite(id);
    }

    @Override
    public boolean addSongList(SongList newSongList, MultipartFile pic) throws IOException {
        String newPic = fileUpload.upload(pic);
        newSongList.setPic(newPic);
        return songListMapper.insert(newSongList)>0;

    }



    @Override
    public boolean deleteSongList(Integer id) {
        return songListMapper.deleteSongListBySongId(id)>0;
    }

    @Override
    public boolean updateSongList(SongList songList, MultipartFile pic) throws IOException {
        String newPic = fileUpload.upload(pic);
        songList.setPic(newPic);
        return songListMapper.updateSongList(songList)>0;
    }


}
